// 本地文件转Base64
export const fileToBase64 = (file) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = () => resolve(reader.result)
    reader.onerror = (error) => reject(error)
    reader.readAsDataURL(file)
  })
}

// 网络图片转Base64
export const urlToBase64 = (url) => {
  return fetch(url)
    .then((response) => response.blob())
    .then((blob) => {
      return new Promise((resolve) => {
        const reader = new FileReader()
        reader.onload = () => resolve(reader.result)
        reader.readAsDataURL(blob)
      })
    })
}

// DOM元素转Base64（需配合html2canvas）
// export const elementToBase64 = async (selector) => {
//   const html2canvas = (await import('html2canvas')).default
//   const canvas = await html2canvas(document.querySelector(selector))
//   return canvas.toDataURL('image/png')
// }
